load(
    "@gz//bazel/skylark:build_defs.bzl",
    "GZ_FEATURES",
    "GZ_ROOT",
    "GZ_VISIBILITY",
    "cmake_configure_file",
    "gz_configure_header",
    "gz_export_header",
    "gz_include_header",
)
load(
    "@gz//bazel/lint:lint.bzl",
    "add_lint_tests",
)

cmake_configure_file(
    name = "build_config",
    src = "src/build_config.hh.in",
    out = "include/build_config.hh",
    cmakelists = ["src/CMakeLists.txt"],
    defines = [
        "SCHEMA_INSTALL_PATH=transport/log/sql",
    ],
)

gz_export_header(
    name = "include/gz/transport/log/Export.hh",
    export_base = "GZ_TRANSPORT_LOG",
    lib_name = "gz-transport-log",
    visibility = ["//visibility:private"],
)

public_headers_no_gen = glob([
    "include/gz/transport/log/*.hh",
    "include/gz/transport/log/detail/*.hh",
])

private_headers = glob(["src/*.hh"])

sources = glob(
    ["src/*.cc"],
    exclude = [
        "src/*_TEST.cc",
    ],
)

public_headers = public_headers_no_gen + [
    "include/gz/transport/log/Export.hh",
]

cc_library(
    name = "log",
    srcs = sources + private_headers + ["include/build_config.hh"],
    hdrs = public_headers,
    data = ["sql/0.1.0.sql"],
    includes = ["include"],
    deps = [
        GZ_ROOT + "transport",
        "@sqlite3",
    ],
)

test_sources = glob(
    include = ["src/*_TEST.cc"],
    exclude = ["src/LogCommandAPI_TEST.cc"],
)

[cc_test(
    name = src.replace("/", "_").replace(".cc", "").replace("src_", ""),
    srcs = [src],
    data = [
        "test/data/state.tlog",
    ],
    defines = [
        'GZ_TRANSPORT_LOG_TEST_PATH=\\"transport/log/test\\"',
        'CORRUPT_DB_TEST_PATH=\\"transport/log/test/data/state.tlog\\"',
    ],
    env = {
        "GZ_BAZEL": "1",
        "GZ_BAZEL_PATH": "transport",
    },
    deps = [
        ":log",
        GZ_ROOT + "common/testing",
        GZ_ROOT + "transport/test:utils",
        "@gtest",
        "@gtest//:gtest_main",
    ],
) for src in test_sources]

add_lint_tests()
